package com.yksj.healthtalk.ui.doctorstation;

import java.util.ArrayList;
import java.util.List;

import org.handmark.pulltorefresh.library.PullToRefreshBase;
import org.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import org.handmark.pulltorefresh.library.PullToRefreshListView;
import org.json.JSONArray;
import org.json.JSONObject;

import sfs2x.client.core.SFSEvent;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.yksj.healthtalk.adapter.DoctorAdapter;
import com.yksj.healthtalk.adapter.DoctorAdapter.onClickFriendAttentionListener;
import com.yksj.healthtalk.adapter.DoctorAdapter.onClickFriendHeadListener;
import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.comm.LodingFragmentDialog;
import com.yksj.healthtalk.entity.CustomerInfoEntity;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.http.ObjectHttpResponseHandler;
import com.yksj.healthtalk.net.socket.SmartFoxClient;
import com.yksj.healthtalk.services.MessagePushService;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.ui.app.AppData;
import com.yksj.healthtalk.ui.app.HTalkApplication;
import com.yksj.healthtalk.ui.doctor.DoctorExactSearchActivity;
import com.yksj.healthtalk.ui.friend.DoctorMainActivity;
import com.yksj.healthtalk.ui.setting.PersonalInformationActivity;
import com.yksj.healthtalk.utils.FriendHttpUtil;
import com.yksj.healthtalk.utils.JsonParseUtils;
import com.yksj.healthtalk.utils.ToastUtil;
/**
 * 我的诊所  我的合作者
 * @author Administrator
 *
 */
public class DoctorMyPartner extends BaseFragmentActivity implements
		OnRefreshListener<ListView>, OnItemClickListener, OnClickListener, onClickFriendAttentionListener, onClickFriendHeadListener {
	private PullToRefreshListView mPullToRefreshListView;
	private ListView mListView;
	private DoctorAdapter adapter;
	private ArrayList<CustomerInfoEntity> entities;
	private CustomerInfoEntity cacheCustomerInfoEntity;
	private AppData mAppData;
	private IntentFilter filter;
	
	//广播接受处理
	private BroadcastReceiver receiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			if (action.equals(MessagePushService.ACTION_COLLECT_FRIEND)) {
				LodingFragmentDialog.dismiss(getSupportFragmentManager());
				String result = intent.getStringExtra("result");
				if (result.equals("0")) {
					ToastUtil.showShort(context, R.string.groupNewFail);
				} else {
					FriendHttpUtil.requestAttentionTofriendsResult(context,cacheCustomerInfoEntity, mAppData);
					adapter.notifyDataSetChanged();
				}
			} else if (action.equals(SFSEvent.CONNECTION_LOST)) {
			}
		}

	};
	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.doctor_my_partner);
		mAppData = HTalkApplication.getAppData();
		initView();

	}

	private void initView() {
		initTitle();
		titleLeftBtn.setOnClickListener(this);
		titleTextV.setText("合作者");
		titleRightBtn2.setVisibility(View.VISIBLE);
		titleRightBtn2.setText("搜索");
		titleRightBtn2.setOnClickListener(this);
		
		mPullToRefreshListView = (PullToRefreshListView) findViewById(android.R.id.list);
		mListView = mPullToRefreshListView.getRefreshableView();
		mPullToRefreshListView.setOnRefreshListener(this);
		mListView.setOnItemClickListener(this);
		
		
		entities = new ArrayList<CustomerInfoEntity>();
		adapter = new DoctorAdapter(this, entities, false);
		mListView.setAdapter(adapter);
		adapter.setonClickFriendAttentionListener(this);
		adapter.setonClickFriendHeadListener(this);
		mPullToRefreshListView.setRefreshing();
		
		initHttpData();
	}

	@Override
	protected void onStart() {
		super.onStart();
		filter = new IntentFilter();
		filter.addAction(MessagePushService.ACTION_COLLECT_FRIEND);
		filter.addAction(SFSEvent.CONNECTION_LOST);
		registerReceiver(receiver, filter);
	}
	@Override
	protected void onStop() {
		super.onStop();
		unregisterReceiver(receiver);
		
	}
	
	private void initHttpData() {
		HttpRestClient.doHttpDoctorMyPartner(SmartFoxClient.getLoginUserId(), 
			new ObjectHttpResponseHandler() {
				@Override
				public Object onParseResponse(String content) {
					try {
						JSONObject object = new JSONObject(content);
						JSONArray jsonArray = object.optJSONArray("MYPARTNERS");
						return JsonParseUtils.jsonfilter(jsonArray.toString());
					} catch (Exception e) {
						return null;
					}
				}
				@Override
				public void onSuccess(int statusCode, Object response) {
					super.onSuccess(statusCode, response);
					if(response!=null && (response instanceof ArrayList)){
						ArrayList<CustomerInfoEntity> infoEntities = (ArrayList<CustomerInfoEntity>) response;
						if (infoEntities.size() == 0) {
							ToastUtil.showToastPanl("暂无内容...");
						}else {
							adapter.setAdapterData(infoEntities);
						}
					}
 				}
				@Override
				public void onFinish() {
					super.onFinish();
					mPullToRefreshListView.onRefreshComplete();
				}
			});

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_back:
			onBackPressed();
			break;
		case R.id.title_right2: //医生精确查找
			Intent intent = new Intent(this, DoctorExactSearchActivity.class);
			startActivity(intent);
		}

	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
			CustomerInfoEntity entity = entities.get(arg2-1);
			if (SmartFoxClient.getLoginUserId().equals(entity.getId())) {
				Intent intent = new Intent();
				intent.setClass(getApplicationContext(), PersonalInformationActivity.class);
				startActivity(intent);
			}else {
				Intent intent = new Intent(getApplicationContext(), DoctorMainActivity.class);
				intent.putExtra("id", entity.getId());
				startActivity(intent);
		}
	}

	@Override
	public void onRefresh(PullToRefreshBase<ListView> refreshView) {
		initHttpData();
	}

	@Override
	public void onClickFriendAttention(int attentioned,
			CustomerInfoEntity entity, int positon) {
		cacheCustomerInfoEntity = FriendHttpUtil.requestAttentionTofriends(this,null, entity);
	}

	@Override
	public void onClickFriendHead(CustomerInfoEntity entity, int positon) {
		if (SmartFoxClient.getLoginUserId().equals(entity.getId())) {
			Intent intent = new Intent();
			intent.setClass(getApplicationContext(), PersonalInformationActivity.class);
			startActivity(intent);
		}else {
			Intent intent = new Intent(getApplicationContext(), DoctorMainActivity.class);
			intent.putExtra("id", entity.getId());
			startActivity(intent);
		}
	}
@Override
protected void onResume() {
	super.onResume();
	initHttpData();
}
}
